var Newfund = {
	form:{},
	validateLogin: function() {
		var userID = jQuery('input#userID').val() || "";
		var success = true;
		if(userID == "" || userID == "-1") {
			if(!Newfund.validateEmail()){
				success = false;
			}
			if(!Newfund.validatePassword()){
				success = false;
			}
		}
		return success;
	},
	
	validateAmount:function() {
		var amount = +Newfund.form.find('input[name=amount]').val() || "";
		var success = true;
		if(amount == ""){
			System.showValidation("Please enter a valid amount.");
			success = false;
		} else if(!isNaN(amount) && amount < 10) {
			System.showValidation("Minimum fund goal is $10.00");
			success = false;
		}
		return success;
	},
	
	validateWhatFor:function() {
		var whatfor = Newfund.form.find('input[name=what_for]').val() || "";
		var success = true;
		if(whatfor == ""){
			System.showValidation("Please describe what the funds will be used for.");
			success = false;
		} else if(whatfor.length < 4) {
			System.showValidation('Please be more descriptive than just "'+whatfor+'".');
			success = false;
		}
		return success;
	},
	
	validateWhoBenefit:function() {
		var whobenefit = Newfund.form.find('input[name=who_benefit]').val() || "";
		var success = true;
		if(whobenefit == ""){
			System.showValidation("Please indicate Who the funds will benefit?");
			success = false;
		} else if(whobenefit.length < 4) {
			System.showValidation('Please be more descriptive than just "'+whobenefit+'".');
			success = false;
		}
		return success;
	},
	
	validateFirstname:function() {
		var firstname = Newfund.form.find('input[name=first_name]').val() || "";
		var success = true;
		if(firstname == ""){
			System.showValidation("Please Enter a First Name.");
			success = false;
		}
		return success;
	},
	
	validateLastname:function() {
		var lastname = Newfund.form.find('input[name=last_name]').val() || "";
		var success = true;
		if(lastname == ""){
			System.showValidation("Please Enter a Last Name.");
			success = false;
		}
		return success;
	},
	
	validateEmail:function() {
		var email = Newfund.form.find('input[name=email]').val() || "";
		var success = true;
		if(email == ""){
			System.showValidation("Please Enter an Email.");
			success = false;
		}
		return success;
	},
	
	validatePassword:function() {
		var password = Newfund.form.find('input[name=password]').val() || "";
		var success = true;
		if(password == ""){
			System.showValidation("Please Enter a Password.");
			success = false;
		}
		return success;
	},

	showStepTwo: function() {
		if(Newfund.validateAmount()){
			System.validationMessages.dialog('close');
			Newfund.form.find('input#next_1').fadeOut();
			Newfund.form.find('tr#createFundRow_2').fadeIn();
			Newfund.form.find('input[name=what_for]').focus();
		}
	},
	
	showStepThree: function() {
		if(Newfund.validateWhatFor() && Newfund.validateWhoBenefit()){
			System.validationMessages.dialog('close');
			Newfund.form.find('input#next_2').fadeOut();
			Newfund.form.find('tr#createFundRow_3').fadeIn();
			Newfund.form.find('textarea[name=description]').focus();
		}
	},

	showStepFour: function() {
		System.validationMessages.dialog('close');
		Newfund.form.find('input#next_3').fadeOut();
		Newfund.form.find('tr#createFundRow_4').fadeIn();
		Newfund.form.find('input[name=first_name]').focus();
	},
	
	showStepFive: function() {
		if(Newfund.validateFirstname() && Newfund.validateLastname()) {
			System.validationMessages.dialog('close');
			Newfund.form.find('input#next_4').fadeOut();
			Newfund.form.find('tr#createFundRow_5').fadeIn();
			Newfund.form.find('input[name=mail_street_1]').focus();
		}
	},
	
	showStepSix: function() {
		if(Newfund.validateStreet() && Newfund.validateCity() && Newfund.validateState() && Newfund.validateZip()) {
			System.validationMessages.dialog('close');
			Newfund.form.find('input#next_5').fadeOut();
			Newfund.form.find('tr#createFundRow_6').fadeIn();
			Newfund.form.find('input#createFundSubmit').focus();
		}
	},
	
	showFundrFee: function(amount) {
		if(!isNaN(amount)){
			Newfund.form.find('span#fundrFee').html((amount * .06).toFixed(2));
		} else {
			Newfund.form.find('span#fundrFee').html("0.00");
		}
	},
	
	showFundrName: function() {
		var what_for = Newfund.form.find('input[name=what_for]').val() || "";
		var who_benefit = Newfund.form.find('input[name=who_benefit]').val() || "";
		var amount = Newfund.form.find('input[name=amount]').val() || "";
		if(amount == "" || isNaN(amount)){amount = "0.00";}
		if(what_for == ""){what_for = "[Some item or service]";}
		if(who_benefit == ""){who_benefit = "[some person or people]";}
		var result = "$"+amount+" towards "+what_for+ " for "+who_benefit;
		Newfund.form.find('input#name').val(result);
		Newfund.form.find('span#fundrName').html(result);
	},
	
	validateStreet: function() {
		var street_1 = Newfund.form.find('input[name=mail_street_1]').val() || "";
		var success = true;
		if(street_1 == "") {
			System.showValidation("Please Enter a street address.");
			success = false;
		}
		return success;
	},
	
	validateCity: function() {
		var city = Newfund.form.find('input[name=mail_city]').val() || "";
		var success = true;
		if(city == "") {
			System.showValidation("Please Enter a City.");
			success = false;
		}
		return success;
	},
	
	validateState: function() {
		var state = Newfund.form.find('select[name=mail_state_id]').val() || "";
		var success = true;
		if(state == "") {
			System.showValidation("Please Enter a State.");
			success = false;
		}
		return success;
	},
	
	validateZip: function() {
		var zip = Newfund.form.find('input[name=mail_zip]').val() || "";
		var success = true;
		if(zip == "") {
			System.showValidation("Please Enter a Zip Code.");
			success = false;
		} else if (zip.length < 5){
			System.showValidation("Please Enter a Valid 5 digit Zip Code.");
			success = false;
		}
		return success;
	},
	
	validateForm: function(){
		var success = true;
		if(!Newfund.validateAmount()){success = false;}
		if(!Newfund.validateWhatFor()){success = false;}
		if(!Newfund.validateWhoBenefit()){success = false;}
		if(!Newfund.validateFirstname()){success = false;}
		if(!Newfund.validateLastname()){success = false;}
		if(!Newfund.validateStreet()){success = false;}
		if(!Newfund.validateCity()){success = false;}
		if(!Newfund.validateState()){success = false;}
		if(!Newfund.validateZip()){success = false;}
		if(!Newfund.validateLogin()){success = false;}
		return success;
	},
	
	show2: function(){
		$('.step1').fadeOut();
		$('td#step2').fadeIn();
	},
	
	show3: function(){
		$('.step2').fadeOut();
		$('td#step3').fadeIn();
	}

};

jQuery(document).ready(function(){
	Newfund.form = jQuery('form#newFundForm');
	Validate.activate(jQuery('form#newFundForm'));
	Newfund.form.find('input[name=amount]').keypress(function(event){
		if(event.which == 13){
			Newfund.showStepTwo();
		}
	});
	
	Newfund.form.find('input[name=what_for]').keypress(function(event){
		if(event.which == 13){
			if(Newfund.validateWhatFor()){
				Newfund.form.find('input[name=who_benefit]').focus();
			}
		}
	});
	
	Newfund.form.find('input[name=who_benefit]').keypress(function(event){
		if(event.which == 13){
			Newfund.showStepThree();
		}
	});
	
	Newfund.form.find('input[name=amount]').keyup(function(){
		Newfund.showFundrFee(this.value);
		Newfund.showFundrName();
	});
	
	Newfund.form.find('input[name=what_for]').keyup(function(){
		Newfund.showFundrName();
	});
	
	Newfund.form.find('input[name=who_benefit]').keyup(function(){
		Newfund.showFundrName();
	});
	
	Newfund.form.find('tr#createFundRow_4').find('input[type=text]').keypress(function(event){
		if(event.which == 13){
			Newfund.showStepFive();
		}
	});
	
	Newfund.form.find('tr#createFundRow_4').find('select').keypress(function(event){
		if(event.which == 13){
			Newfund.showStepFive();
		}
	});
});